﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.Models;
using QLBH.Utilities;
using QLBH.Modules;
using System.Windows.Forms;

namespace QLBH.Controllers
{
    class OrdersController
    {
        private OrdersModel ObjModel;
        private ucOrders ViewOrders;
        public OrdersController(ucOrders ViewOrders)
        {
            ObjModel = new OrdersModel();
            this.ViewOrders = ViewOrders;
        }

        internal void LoadData()
        {
            try
            {
                List<OrdersModel> data = WSAccess.GetData<List<OrdersModel>>("orders/list/" + MainForm.Instance.Shop_id.ToString());

                ViewOrders.DisplayData(data);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        internal void Add()
        {
            frmAdd_Edit_Orders frmOrder = new frmAdd_Edit_Orders(eType.ADD);
            frmOrder.Text = "Thêm mới hóa đơn";
            if (frmOrder.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        internal void Edit()
        {
            frmAdd_Edit_Orders frmOrder = new frmAdd_Edit_Orders(eType.EDIT);
            frmOrder.Text = "Sửa thông tin hóa đơn";
            try
            {
                if (ViewOrders.SelectItem() != null)
                {
                    frmOrder.objModel = ViewOrders.SelectItem();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (frmOrder.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
    }
}
